package com.guoxue.action.front;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.guoxue.dao.impl.TapesRecordsDaoImpl;
import com.guoxue.domain.TapesRecordsVo;
import com.guoxue.domain.TapesShareVo;
import com.guoxue.domain.UploadLogsVo;
import com.guoxue.model.RedisUser;
import com.guoxue.service.ITapesRecordsService;
import com.guoxue.service.ITapesShareService;
import com.guoxue.service.IUploadLogsService;
import com.guoxue.util.RedisUtil;
import com.guoxue.util.MD5Util;
import com.guoxue.util.OSSUtil;
import com.guoxue.util.OtherUtils;

import redis.clients.jedis.Jedis;
/**
 * 
 * @author young
 *
 */
@Controller
@RequestMapping("Util")
public class UtilAction extends BaseAction{
	@Resource
	private OSSUtil oss;
	private static final String bucket = "bykj-gx-oss-bucket1";
	@Resource
	private ITapesRecordsService tapesService;
	@Resource
	private IUploadLogsService logService;
	@Resource
	private ITapesShareService shareService;
	
	@RequestMapping("/cleanTapes")
	@ResponseBody
	public JSON cleanTapesByTime(HttpServletRequest req, HttpServletResponse resp){
		Calendar c = Calendar.getInstance();
	    c.add(Calendar.MONTH, -1);
	    String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(c.getTime());
	    List<TapesRecordsVo> tr = tapesService.findByTimeAndStatus(date); 
	    System.err.println(date);
	    if (tr.size()!=0){
	    	for (TapesRecordsVo t:tr){
	    		oss.deleteObject(bucket, t.getTapes());
	    		tapesService.delete(t);
	    	}
	    }
		return null;
	}
	/**
	 * 获取全局唯一key
	 * @param req
	 * @param resp
	 * @param uid
	 * @return
	 */
	@RequestMapping("/getKey")
	@ResponseBody
	public JSON getKey(HttpServletRequest req, HttpServletResponse resp, Integer uid){
		if (uid==null){
			return Message(ERROR, "获取全局key失败，未提交用户id", null);
		}
		MD5Util.generatorKey(uid);
		return Message(SUCCESS, "获取全局key成功", MD5Util.generatorKey(uid));
	}
	
	@RequestMapping("listenCount")
	@ResponseBody
	public JSON listenCount(HttpServletRequest req, HttpServletResponse resp, Integer uid, Integer tapesId){
		if (uid==null){
			return Message(ERROR, "未提交用户id", null);
		}
		if (tapesId==null){
			return Message(ERROR, "未提交音频id", null);
		}
		Jedis jedis = RedisUtil.getJedis();
		RedisUser redisUser =(RedisUser) RedisUtil.unserialize(jedis.get((tapesId+"R"+uid).getBytes()));
		int destroy = OtherUtils.subtractDate(new Date());
		boolean boo = false;
		if (redisUser==null){
			redisUser = new RedisUser();
			boo = true;
		}else if (redisUser.getCount()<3){
			boo = true;
		} 
		if (boo) {
			TapesShareVo share = shareService.getTapesShareByTid(tapesId, 0);
			if (share==null){
				share = new TapesShareVo();
				share.setTapesId(tapesId);
				share.setListened(1);
			} else {
				share.setListened(share.getListened()+1);
			}
			redisUser.setCount(redisUser.getCount()+1);
			shareService.saveTapesShare(share);
			jedis.setex((tapesId+"R"+uid).getBytes(), destroy,RedisUtil.serialize(redisUser));
		}
		return null;
	}
}
